package es.cazadoresmadridejos.util;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import es.cazadoresmadridejos.model.Photo.PhotoType;

public class FileUtils {

	public enum FileType{
		ADVICED,PHOTO,PROFILE, ADVERTISEMENT
	}

	public static String saveForm(CommonsMultipartFile fileForm,FileType fileType) throws Exception{
		return saveForm(fileForm, fileType, null, null);
	}
	
	public static String saveForm(CommonsMultipartFile fileForm,FileType fileType, Long partnerId) throws Exception{
		return saveForm(fileForm, fileType, null, partnerId);
	}
	public static String saveForm(CommonsMultipartFile fileForm,FileType fileType, PhotoType photoType) throws Exception{
		return saveForm(fileForm, fileType, photoType, null);
	}
	
	public static String saveForm(CommonsMultipartFile fileForm,FileType fileType, PhotoType photoType, Long partnerId) throws Exception{
		if(fileForm == null || StringUtils.isEmpty(fileForm.getOriginalFilename())){
			return null;
		}
		
		String name = System.currentTimeMillis() + "_" + fileForm.getOriginalFilename().replaceAll(" ", "_")
				.replaceAll("á", "a")
				.replaceAll("é", "e")
				.replaceAll("í", "i")
				.replaceAll("ó", "o")
				.replaceAll("ú", "u")
				.replaceAll("ä", "a")
				.replaceAll("ë", "e")
				.replaceAll("ï", "i")
				.replaceAll("ö", "o")
				.replaceAll("ü", "u")
				.replaceAll("à", "a")
				.replaceAll("è", "e")
				.replaceAll("ì", "i")
				.replaceAll("ò", "o")
				.replaceAll("ù", "u")
				.replaceAll("ñ", "n")
				.replaceAll("`", "'")
				.replaceAll("<", "[")
				.replaceAll(">", "]")
				.replaceAll("!", "")
				.replaceAll("¡", "");
		
		String basePath = System.getProperty("catalina.home");
		String filePath = basePath;
		if(fileType.equals(FileType.PHOTO) ){
			basePath += File.separator + "webapps" + File.separator + "photos" + File.separator + photoType.name();
			File file = new File(basePath);
			if(!file.exists()){
				file.mkdir();
			}
		}
		if(fileType.equals(FileType.PROFILE) ){
			basePath += File.separator + "webapps" + File.separator + "photos" + File.separator + "USER";
			File file = new File(basePath);
			if(!file.exists()){
				file.mkdir();
			}
			name = "user_" + partnerId;
		}
		if(fileType.equals(FileType.ADVICED) ){
			
			basePath += File.separator + fileType.name();
			File file = new File(basePath);
			if(!file.exists()){
				file.mkdir();
			}
		}
		if(fileType.equals(FileType.ADVERTISEMENT) ){
			
			basePath += File.separator + "webapps" + File.separator + "photos" +File.separator + FileType.ADVERTISEMENT.name() ;
			File file = new File(basePath);
			if(!file.exists()){
				file.mkdir();
			}
		}

		filePath = basePath + File.separator + name;

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(filePath));
			fos.write(fileForm.getFileItem().get());
			fos.close();

		} catch (Exception e) {
			throw new Exception("No se ha podido guardar el archico");
		}

		return name;
	}

	public static void deleteAdvertisement(String image) {
		String basePath = System.getProperty("catalina.home") + File.separator + "webapps" + File.separator + "photos" +File.separator + FileType.ADVERTISEMENT.name() + File.separator + image;
		File file = new File(basePath);
		file.delete();
	}

	public static String saveAvatar(CommonsMultipartFile file, Long id) throws Exception {
		String basePath = System.getProperty("catalina.home");
		String fileName = "partner_"+id+"." + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
		basePath += File.separator + "webapps" + File.separator + "photos" + File.separator + "AVATARES" + File.separator + fileName;

		FileOutputStream fos = null;
		try {
			File f= new File(basePath);
			
			if(f.exists()){
				f.delete();
				f.createNewFile();
			}
			
			fos = new FileOutputStream(f,false);
			fos.write(file.getFileItem().get());
			fos.close();

		} catch (Exception e) {
			throw new Exception("No se ha podido guardar el archico");
		}
		
		return fileName;
	}
}
